package cn.admobiletop.adsuyi.adapter.toutiao.loader;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;

import cn.admobiletop.adsuyi.ADSuyiSdk;
import cn.admobiletop.adsuyi.ad.ADSuyiInterstitialAd;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterLoader;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterParams;
import cn.admobiletop.adsuyi.ad.data.ADSuyiPlatformPosId;
import cn.admobiletop.adsuyi.ad.error.ADSuyiError;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiInterstitialAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.ADSuyiIniter;
import cn.admobiletop.adsuyi.adapter.toutiao.listener.InterstitialAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.manager.TTAdInitManager;
import cn.admobiletop.adsuyi.adapter.toutiao.util.TTAdUtil;
import cn.admobiletop.adsuyi.config.ADSuyiInitConfig;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;

/**
 * @author ciba
 * @description 插屏广告加载器
 * @date 2020/4/3
 */
public class InterstitialAdLoader implements ADSuyiAdapterLoader<ADSuyiInterstitialAd, ADSuyiInterstitialAdListener> {
    private InterstitialAdListener interstitialAdListener;

    @Override
    public void loadAd(ADSuyiInterstitialAd interstitialAd, ADSuyiAdapterParams adapterParams, ADSuyiInterstitialAdListener adListener) {
        if (!ADSuyiAdUtil.isReleased(interstitialAd)
                && adapterParams != null
                && adapterParams.getPlatformPosId() != null
                && adListener != null) {
            ADSuyiPlatformPosId platformPosId = adapterParams.getPlatformPosId();

            ADSuyiInitConfig config = ADSuyiSdk.getInstance().getConfig();
            if (config != null && config.isFilterThirdQuestion()) {
                if (TTAdUtil.withoutLowSystem()) {
                    adListener.onAdFailed(ADSuyiError.createErrorDesc(ADSuyiIniter.PLATFORM, platformPosId.getPlatformPosId(), -1, "过滤Interstitial广告，经过测试头条的广告在安卓5.1及以下版本的手机上可能存在兼容性问题"));
                    return;
                }
            }

            TTAdNative ttAdNative = TTAdInitManager.getInstance().createNative(interstitialAd.getActivity());
            if (ttAdNative == null) {
                adListener.onAdFailed(ADSuyiError.createErrorDesc(ADSuyiIniter.PLATFORM, platformPosId.getPlatformPosId(), -1, "头条SDK createNative失败，可能初始化失败或初始化数据有误"));
                return;
            }

            express(interstitialAd, platformPosId, adListener, ttAdNative);
        }
    }

    /**
     * 模板渲染插屏
     * @param interstitialAd
     * @param platformPosId
     * @param adListener
     * @param ttAdNative
     */
    private void express(ADSuyiInterstitialAd interstitialAd, ADSuyiPlatformPosId platformPosId, ADSuyiInterstitialAdListener adListener, TTAdNative ttAdNative) {
        // 广告请求参数
        AdSlot adSlot = new AdSlot.Builder()
                // 广告位id
                .setCodeId(platformPosId.getPlatformPosId())
                // 支持跳转打开其他app
                .setSupportDeepLink(true)
                // 请求广告数量为1到3条
                .setAdCount(1)
                // 期望模板广告view的size,单位dp
                .setExpressViewAcceptedSize(340, 0)
                // 这个参数设置即可，不影响模板广告的size
                .setImageAcceptedSize(640, 320)
                .setDownloadType(TTAdUtil.getDisplayDownloadTipsDialogType())
                .build();
        interstitialAdListener = new InterstitialAdListener(platformPosId.getPlatformPosId(), interstitialAd, adListener);
        ttAdNative.loadInteractionExpressAd(adSlot, interstitialAdListener);
    }

    @Override
    public void onResumed() {

    }

    @Override
    public void onPaused() {

    }

    @Override
    public void release() {
        if (interstitialAdListener != null) {
            interstitialAdListener.release();
            interstitialAdListener = null;
        }
    }
}
